<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hadoop ���򿪷� | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Hadoop程序开发   一、实验目的  Hadoop 程序开发 二、实验内容（实验过程、步骤及实验结果）   创建Rectangle.txt文件，输入如下数据  123459 94 57 81 13 6 启动hadoop  1start-all.sh 在HDFS上创建目录&#x2F;input&#x2F;sort，并将Rectangle.txt文件上传其中。  12hadoop fs -mkdir &#x2F;in">
<meta property="og:type" content="article">
<meta property="og:title" content="Hadoop ���򿪷�">
<meta property="og:url" content="http://example.com/2020/12/18/Hadoop%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Hadoop程序开发   一、实验目的  Hadoop 程序开发 二、实验内容（实验过程、步骤及实验结果）   创建Rectangle.txt文件，输入如下数据  123459 94 57 81 13 6 启动hadoop  1start-all.sh 在HDFS上创建目录&#x2F;input&#x2F;sort，并将Rectangle.txt文件上传其中。  12hadoop fs -mkdir &#x2F;in">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="c:/Users/chenhongda/AppData/Roaming/Typora/typora-user-images/1601815397125.png">
<meta property="og:image" content="c:/Users/chenhongda/AppData/Roaming/Typora/typora-user-images/1601815407046.png">
<meta property="article:published_time" content="2020-12-18T01:22:33.598Z">
<meta property="article:modified_time" content="2020-12-18T01:22:33.598Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/chenhongda/AppData/Roaming/Typora/typora-user-images/1601815397125.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Hadoop程序开发" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/12/18/Hadoop%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/" class="article-date">
  <time class="dt-published" datetime="2020-12-18T01:22:33.598Z" itemprop="datePublished">2020-12-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Hadoop ���򿪷�
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <center><h1>Hadoop程序开发</h1></center>


<h2>一、实验目的</h2>

<p>Hadoop 程序开发</p>
<h2>二、实验内容（实验过程、步骤及实验结果）</h2>

<h4> 创建Rectangle.txt文件，输入如下数据</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">9 9</span><br><span class="line">4 5</span><br><span class="line">7 8</span><br><span class="line">1 1</span><br><span class="line">3 6</span><br></pre></td></tr></table></figure>
<h4>启动hadoop</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">start-all.sh</span><br></pre></td></tr></table></figure>
<h4>在HDFS上创建目录/input/sort，并将Rectangle.txt文件上传其中。</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">hadoop fs -mkdir &#x2F;input&#x2F;sort</span><br><span class="line">hadoop fs -put &#x2F;data&#x2F;Rectangle.txt &#x2F;input&#x2F;sort</span><br></pre></td></tr></table></figure>
<h4>建立RectangleSort类</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line">package sds.mapreduce;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F;导包 </span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.net.URI;</span><br><span class="line">import java.net.URISyntaxException;</span><br><span class="line"> </span><br><span class="line">import org.apache.hadoop.conf.Configuration;</span><br><span class="line">import org.apache.hadoop.fs.FileSystem;</span><br><span class="line">import org.apache.hadoop.fs.Path;</span><br><span class="line">import org.apache.hadoop.io.IntWritable;</span><br><span class="line">import org.apache.hadoop.io.LongWritable;</span><br><span class="line">import org.apache.hadoop.io.NullWritable;</span><br><span class="line">import org.apache.hadoop.io.Text;</span><br><span class="line">import org.apache.hadoop.mapreduce.Job;</span><br><span class="line">import org.apache.hadoop.mapreduce.Mapper;</span><br><span class="line">import org.apache.hadoop.mapreduce.Partitioner;</span><br><span class="line">import org.apache.hadoop.mapreduce.Reducer;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;</span><br><span class="line"> </span><br><span class="line">public class RectangleSort &#123;</span><br><span class="line">    &#x2F;&#x2F;指定输入输出文件位置</span><br><span class="line">	static final String INPUT_PATH &#x3D; &quot;hdfs:&#x2F;&#x2F;localhost:9000&#x2F;input&#x2F;sort&#x2F;Rectangle.txt&quot;;</span><br><span class="line">	static final String OUTPUT_PATH &#x3D; &quot;hdfs:&#x2F;&#x2F;localhost:9000&#x2F;output&#x2F;rectangle&quot;;</span><br><span class="line"> </span><br><span class="line">	public static void main(String[] args) throws Throwable, URISyntaxException &#123;</span><br><span class="line">	    &#x2F;&#x2F;创建配置类</span><br><span class="line">		Configuration conf &#x3D; new Configuration();</span><br><span class="line">	</span><br><span class="line">		FileSystem fileSystem &#x3D; FileSystem.get(new URI(INPUT_PATH), conf);</span><br><span class="line">		Path outpath &#x3D; new Path(OUTPUT_PATH);</span><br><span class="line">		if (fileSystem.exists(outpath)) &#123;</span><br><span class="line">			fileSystem.delete(outpath, true);</span><br><span class="line">		&#125;</span><br><span class="line"> </span><br><span class="line">		Job job &#x3D; Job.getInstance(conf, &quot;RectangleSort&quot;);</span><br><span class="line">		job.setJarByClass(RectangleWritable.class);</span><br><span class="line">		job.setInputFormatClass(TextInputFormat.class); &#x2F;&#x2F;指定如何对输入的文件格式化，把输入文件每一行解析成键值对</span><br><span class="line">		job.setMapperClass(MyMapper.class);            &#x2F;&#x2F;指定自定义的map类</span><br><span class="line">		job.setMapOutputKeyClass(RectangleWritable.class); &#x2F;&#x2F;map输出的&lt;k,v&gt;类型，如果&lt;k3,v3&gt;的类型与&lt;k2,v2&gt;类型一致，则可以省略</span><br><span class="line">		job.setMapOutputValueClass(NullWritable.class);</span><br><span class="line">		job.setReducerClass(MyReducer.class);   &#x2F;&#x2F;指定自定义reduce类</span><br><span class="line">		job.setOutputKeyClass(IntWritable.class);</span><br><span class="line">		job.setOutputValueClass(IntWritable.class);</span><br><span class="line">		FileInputFormat.setInputPaths(job, INPUT_PATH);</span><br><span class="line">		FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));</span><br><span class="line">		job.setOutputFormatClass(TextOutputFormat.class);   &#x2F;&#x2F;指定输出文件的格式化类分区</span><br><span class="line">		job.setPartitionerClass(MyPatitioner.class);</span><br><span class="line">		job.setNumReduceTasks(2); &#x2F;&#x2F;指定两个reduce任务运行</span><br><span class="line">		job.waitForCompletion(true);  &#x2F;&#x2F;把job提交给jobtracker运行</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	static class MyMapper extends </span><br><span class="line">			Mapper&lt;LongWritable, Text, RectangleWritable, NullWritable&gt; &#123;</span><br><span class="line"> </span><br><span class="line">		protected void map(LongWritable k1, Text v1, Context context)</span><br><span class="line">				throws IOException, InterruptedException &#123;</span><br><span class="line">			String[] splites &#x3D; v1.toString().split(&quot; &quot;);</span><br><span class="line">			RectangleWritable k2 &#x3D; new RectangleWritable(</span><br><span class="line">					Integer.parseInt(splites[0]), Integer.parseInt(splites[1]));</span><br><span class="line">			context.write(k2, NullWritable.get());</span><br><span class="line">		&#125;;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	static class MyReducer extends</span><br><span class="line">			Reducer&lt;RectangleWritable, NullWritable, IntWritable, IntWritable&gt; &#123;</span><br><span class="line">		protected void reduce(RectangleWritable k2, Iterable&lt;NullWritable&gt; v2s,</span><br><span class="line">				Context context) throws IOException, InterruptedException &#123;</span><br><span class="line">			context.write(new IntWritable(k2.getLength()),</span><br><span class="line">					new IntWritable(k2.getWidth()));</span><br><span class="line">		&#125;;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">class MyPatitioner extends Partitioner&lt;RectangleWritable, NullWritable&gt; &#123;</span><br><span class="line">	@Override</span><br><span class="line">	&#x2F;&#x2F;重写获取分区方法</span><br><span class="line">	public int getPartition(RectangleWritable k2, NullWritable v2,</span><br><span class="line">			int numReduceTasks) &#123;</span><br><span class="line"> </span><br><span class="line">		if (k2.getLength() &#x3D;&#x3D; k2.getWidth()) &#123;</span><br><span class="line">			return 0; &#x2F;&#x2F; 正方形在任务0中汇总</span><br><span class="line">		&#125; else</span><br><span class="line">			return 1;&#x2F;&#x2F; 长方形在任务1中汇总</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4>继承WritableComparable类</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">package sds.mapreduce; </span><br><span class="line"> </span><br><span class="line">import java.io.DataInput;</span><br><span class="line">import java.io.DataOutput;</span><br><span class="line">import java.io.IOException;</span><br><span class="line"> </span><br><span class="line">import org.apache.hadoop.io.WritableComparable;</span><br><span class="line"> </span><br><span class="line">public class RectangleWritable implements WritableComparable &lt;RectangleWritable&gt; &#123;</span><br><span class="line"> </span><br><span class="line">	int length, width;</span><br><span class="line"> </span><br><span class="line">	public int getLength() &#123;</span><br><span class="line">		return length;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public void setLength(int length) &#123;</span><br><span class="line">		this.length &#x3D; length;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public int getWidth() &#123;</span><br><span class="line">		return width;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public void setWidth(int width) &#123;</span><br><span class="line">		this.width &#x3D; width;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public RectangleWritable() &#123;</span><br><span class="line">		super();</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public RectangleWritable(int length, int width) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.length &#x3D; length;</span><br><span class="line">		this.width &#x3D; width;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	@Override</span><br><span class="line">	public void write(DataOutput out) throws IOException &#123;</span><br><span class="line">		out.writeInt(length);</span><br><span class="line">		out.writeInt(width);</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	@Override</span><br><span class="line">	public void readFields(DataInput in) throws IOException &#123;</span><br><span class="line">		this.length &#x3D; in.readInt();</span><br><span class="line">		this.width &#x3D; in.readInt();</span><br><span class="line"> </span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	@Override</span><br><span class="line">	public int compareTo(RectangleWritable o) &#123;</span><br><span class="line">		if (this.getLength() * this.getWidth() &gt; o.getLength() * o.getWidth())</span><br><span class="line">			return 1;</span><br><span class="line">		if (this.getLength() * this.getWidth() &lt; o.getLength() * o.getWidth())</span><br><span class="line">			return -1;</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4>建立WordCount类</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">package sds.mapreduce;</span><br><span class="line"> </span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.StringTokenizer;</span><br><span class="line"> </span><br><span class="line">import org.apache.hadoop.conf.Configuration;</span><br><span class="line">import org.apache.hadoop.fs.Path;</span><br><span class="line">import org.apache.hadoop.io.IntWritable;</span><br><span class="line">import org.apache.hadoop.io.Text;</span><br><span class="line">import org.apache.hadoop.mapreduce.Job;</span><br><span class="line">import org.apache.hadoop.mapreduce.Mapper;</span><br><span class="line">import org.apache.hadoop.mapreduce.Reducer;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;</span><br><span class="line">import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;</span><br><span class="line">import org.apache.hadoop.util.GenericOptionsParser;</span><br><span class="line"> </span><br><span class="line">public class WordCount &#123;</span><br><span class="line"> </span><br><span class="line">	public static class TokenizerMapper extends Mapper&lt;Object, Text, Text, IntWritable&gt; &#123;</span><br><span class="line"> </span><br><span class="line">		private final static IntWritable one &#x3D; new IntWritable(1);</span><br><span class="line">		private Text word &#x3D; new Text(); </span><br><span class="line">		public void map(Object key, Text value, Context context) throws IOException, InterruptedException &#123;</span><br><span class="line">			StringTokenizer itr &#x3D; new StringTokenizer(value.toString());</span><br><span class="line">			while (itr.hasMoreTokens()) &#123;</span><br><span class="line">				word.set(itr.nextToken());</span><br><span class="line">				context.write(word, one);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public static class IntSumReducer extends Reducer&lt;Text, IntWritable, Text, IntWritable&gt; &#123;</span><br><span class="line">		private IntWritable result &#x3D; new IntWritable();</span><br><span class="line"> 		public void reduce(Text key, Iterable&lt;IntWritable&gt; values, Context context) throws IOException, InterruptedException &#123;</span><br><span class="line">			int sum &#x3D; 0;</span><br><span class="line">			for (IntWritable val : values) &#123;</span><br><span class="line">				sum +&#x3D; val.get();</span><br><span class="line">			&#125;</span><br><span class="line">			result.set(sum);</span><br><span class="line">			context.write(key, result);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	public static void main(String[] args) throws Exception &#123;</span><br><span class="line">		Configuration conf &#x3D; new Configuration();</span><br><span class="line">		String[] otherArgs &#x3D; new GenericOptionsParser(conf, args).getRemainingArgs();</span><br><span class="line">		if (otherArgs.length &lt; 2) &#123;</span><br><span class="line">			System.err.println(&quot;Usage: wordcount &lt;in&gt; [&lt;in&gt;...] &lt;out&gt;&quot;);</span><br><span class="line">			System.exit(2);</span><br><span class="line">		&#125;</span><br><span class="line">		Job job &#x3D; Job.getInstance(conf, &quot;word count&quot;);</span><br><span class="line">		job.setJarByClass(WordCount.class);</span><br><span class="line">		job.setMapperClass(TokenizerMapper.class);</span><br><span class="line">		job.setCombinerClass(IntSumReducer.class);</span><br><span class="line">		job.setReducerClass(IntSumReducer.class);</span><br><span class="line">		job.setOutputKeyClass(Text.class);</span><br><span class="line">		job.setOutputValueClass(IntWritable.class);</span><br><span class="line">		for (int i &#x3D; 0; i &lt; otherArgs.length - 1; ++i) &#123;</span><br><span class="line">			FileInputFormat.addInputPath(job, new Path(otherArgs[i]));</span><br><span class="line">		&#125;</span><br><span class="line">		FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));</span><br><span class="line">		System.exit(job.waitForCompletion(true) ? 0 : 1);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4>在自己家目录下创建~/project_jar目录用于保存导出的jar包</h4>

<p><img src="C:\Users\chenhongda\AppData\Roaming\Typora\typora-user-images\1601815397125.png" alt="1601815397125"></p>
<p><img src="C:\Users\chenhongda\AppData\Roaming\Typora\typora-user-images\1601815407046.png" alt="1601815407046"></p>
<h4>运行jar包中的java类</h4>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">hadoop jar ~&#x2F;project_jar&#x2F;mr_example.jar sds.mapreduce.WordCount &#x2F;input&#x2F;wordcount&#x2F;testfile &#x2F;output&#x2F;wordcount </span><br><span class="line"></span><br><span class="line">## 运行结果 hadoop fs -ls &#x2F;output&#x2F;wordcount&#x2F;part-r-00000</span><br><span class="line">big    1</span><br><span class="line">data   1</span><br><span class="line">hadoop 1</span><br><span class="line">hdfs   1</span><br><span class="line">hello  3</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">hadoop jar ~&#x2F;project_jar&#x2F;mr_example.jar sds.mapreduce.RectangleSort </span><br><span class="line">&#x2F;&#x2F;配置参数已经写入java类中,无需在命令行中再说明，输出文件也在java类中做了处理。</span><br><span class="line"></span><br><span class="line">## 运行结果 hadoop fs -ls &#x2F;output&#x2F;rectangle&#x2F;part-r-00000</span><br><span class="line">1 1</span><br><span class="line">9 9</span><br><span class="line"></span><br><span class="line">## 运行结果 hadoop fs -ls &#x2F;output&#x2F;rectangle&#x2F;part-r-00001</span><br><span class="line">3 6</span><br><span class="line">4 5</span><br><span class="line">7 8</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/12/18/Hadoop%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/" data-id="ckitlg0ss00014cn66jk1fmdv" data-title="Hadoop ���򿪷�" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/18/hive%E9%AB%98%E7%BA%A7/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Hive高级用法
        
      </div>
    </a>
  
  
    <a href="/2020/12/18/spark%E5%AE%89%E8%A3%85/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Spark</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/12/18/hive%E9%AB%98%E7%BA%A7/">Hive高级用法</a>
          </li>
        
          <li>
            <a href="/2020/12/18/Hadoop%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/">Hadoop ���򿪷�</a>
          </li>
        
          <li>
            <a href="/2020/12/18/spark%E5%AE%89%E8%A3%85/">Spark</a>
          </li>
        
          <li>
            <a href="/2020/12/18/%E6%88%91%E7%9A%84%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/">我的第一篇博客</a>
          </li>
        
          <li>
            <a href="/2020/12/18/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2020 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>